using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Controls;
using Converters;
using MainView.MainControls;
using ProjectView.ProjectControls;
using ProjectView.RuleView;
using SExperiment;
using SProject;
using SProject.Paramter;
using SProject.Program;

namespace MainView.Windows
{
	public partial class SubsetParamterWindow : Window, IComponentConnector
	{
		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(SubsetParamterWindow), new UIPropertyMetadata(true));

		public static readonly DependencyProperty SubsetParamterTypeProperty = DependencyProperty.Register("SubsetParamterType", typeof(ESubsetParamterType), typeof(SubsetParamterWindow), new UIPropertyMetadata(ESubsetParamterType.BasicOption));

		public static readonly DependencyProperty SubsetProperty = DependencyProperty.Register("Subset", typeof(Subset), typeof(SubsetParamterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty SelectedWellsProperty = DependencyProperty.Register("SelectedWells", typeof(List<Well>), typeof(SubsetParamterWindow), new UIPropertyMetadata(null));

		private Hashtable htObject = new Hashtable();

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		public ESubsetParamterType SubsetParamterType
		{
			get
			{
				return (ESubsetParamterType)GetValue(SubsetParamterTypeProperty);
			}
			set
			{
				SetValue(SubsetParamterTypeProperty, value);
			}
		}

		public Subset Subset
		{
			get
			{
				return (Subset)GetValue(SubsetProperty);
			}
			set
			{
				SetValue(SubsetProperty, value);
			}
		}

		public List<Well> SelectedWells
		{
			get
			{
				return (List<Well>)GetValue(SelectedWellsProperty);
			}
			set
			{
				SetValue(SelectedWellsProperty, value);
			}
		}

		public SubsetParamterWindow()
		{
			InitializeComponent();
		}

		private void Root_Loaded(object sender, RoutedEventArgs e)
		{
			if (Subset == null || SelectedWells == null)
			{
				return;
			}
			List<Well> wells = Subset.Wells;
			IList<Project> list = (from s in wells
				select s.Project into s
				where s != null
				select s).Distinct().ToList();
			foreach (Project item in list)
			{
				Subset.SubsetParamter subsetParamter = Subset.GetSubsetParamter(item);
				if (subsetParamter == null)
				{
					continue;
				}
				switch (SubsetParamterType)
				{
				case ESubsetParamterType.BasicOption:
				case ESubsetParamterType.MeltingOption:
				case ESubsetParamterType.HRMOption:
				case ESubsetParamterType.ADOption:
				{
					BasicOption basicOption = OptionFactory.CreateBasicOption(item.BasicInfo.Type);
					if (basicOption is MeltingOption && item.PCRProgram.Programs.Where((BaseSegment s) => s is MeltingSegment).Count() == 0)
					{
						basicOption = OptionFactory.CreateBasicOption(EProjectType.AQ);
					}
					basicOption.Copy(subsetParamter.BasicOption);
					htObject.Add(item, basicOption);
					break;
				}
				case ESubsetParamterType.Detection:
					if (subsetParamter.BasicOption.Items.Where((BasicOptionItem s) => s.AnalysisType == EAnalysisType.ANALYSIS_QUANTITATIVE).Count() != 0)
					{
						BasicOption basicOption = OptionFactory.CreateBasicOption(item.BasicInfo.Type);
						basicOption.Copy(subsetParamter.BasicOption);
						htObject.Add(item, basicOption);
					}
					break;
				case ESubsetParamterType.CrossTalk:
					htObject.Add(item, new CrossTalk(subsetParamter.CrossTalk));
					break;
				case ESubsetParamterType.Rule:
				{
					Project project = new Project();
					project.BasicInfo = item.BasicInfo;
					project.Target = item.Target;
					project.BasicOption = item.BasicOption;
					project.Rule = subsetParamter.Rule.Clone();
					htObject.Add(item, project);
					break;
				}
				}
			}
			if (htObject.Count == 0)
			{
				return;
			}
			foreach (Project key in htObject.Keys)
			{
				Control control = CreateParamterControl(key, htObject[key]);
				if (list.Count == 1)
				{
					tabControl.Visibility = Visibility.Collapsed;
					grid.Children.Add(control);
					continue;
				}
				tabControl.Visibility = Visibility.Visible;
				TabItem tabItem = new TabItem();
				tabItem.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem.Content = control;
				tabItem.Header = key.BasicInfo.Name;
				if (SelectedWells.Count > 0 && SelectedWells[0].Project == key)
				{
					tabItem.IsSelected = true;
				}
				tabControl.Items.Add(tabItem);
			}
			if (tabControl.SelectedIndex == -1 && tabControl.Items.Count > 0)
			{
				tabControl.SelectedIndex = 0;
			}
			tabControl.RefreshTabItemStyle();
		}

		private UCProjectBasicOption CreateBasicOptionControl(Project project, object paramter)
		{
			UCProjectBasicOption uCProjectBasicOption = new UCProjectBasicOption();
			uCProjectBasicOption.BasicOption = (BasicOption)paramter;
			UCProjectBasicOption uCProjectBasicOption2 = uCProjectBasicOption;
			uCProjectBasicOption2.SetBinding(UCProjectBasicOption.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCProjectBasicOption2;
		}

		private UCProjectDetection CreateDetectionControl(Project project, object paramter)
		{
			UCProjectDetection uCProjectDetection = new UCProjectDetection();
			uCProjectDetection.BasicOption = (BasicOption)paramter;
			UCProjectDetection uCProjectDetection2 = uCProjectDetection;
			uCProjectDetection2.SetBinding(UCProjectDetection.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCProjectDetection2;
		}

		private UCMeltingOption CreateMeltingOptionControl(Project project, object paramter)
		{
			UCMeltingOption uCMeltingOption = new UCMeltingOption();
			uCMeltingOption.DataContext = paramter;
			uCMeltingOption.Project = project;
			UCMeltingOption uCMeltingOption2 = uCMeltingOption;
			uCMeltingOption2.SetBinding(UCMeltingOption.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCMeltingOption2;
		}

		private UCHRMOption CreateHRMOptionControl(Project project, object paramter)
		{
			UCHRMOption uCHRMOption = new UCHRMOption();
			uCHRMOption.DataContext = paramter;
			uCHRMOption.Project = project;
			UCHRMOption uCHRMOption2 = uCHRMOption;
			uCHRMOption2.SetBinding(UCHRMOption.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCHRMOption2;
		}

		private UCADOption CreateADOptionControl(Project project, object paramter)
		{
			UCADOption uCADOption = new UCADOption();
			uCADOption.DataContext = paramter;
			UCADOption uCADOption2 = uCADOption;
			uCADOption2.SetBinding(UCADOption.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCADOption2;
		}

		private UCProjectCrossTalk CreateCrossTalkControl(Project project, object paramter)
		{
			UCProjectCrossTalk uCProjectCrossTalk = new UCProjectCrossTalk();
			uCProjectCrossTalk.CrossTalk = (CrossTalk)paramter;
			UCProjectCrossTalk uCProjectCrossTalk2 = uCProjectCrossTalk;
			uCProjectCrossTalk2.SetBinding(UCProjectCrossTalk.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCProjectCrossTalk2;
		}

		private UCRule CreateRuleControl(Project project, object paramter)
		{
			UCRule uCRule = new UCRule();
			uCRule.Project = (Project)paramter;
			UCRule uCRule2 = uCRule;
			uCRule2.SetBinding(UCRule.IsReadOnlyProperty, new Binding("IsDecrypt")
			{
				Source = this,
				Converter = new BoolReverseConverter()
			});
			return uCRule2;
		}

		private Control CreateParamterControl(Project project, object paramter)
		{
			Control result = null;
			if (SubsetParamterType == ESubsetParamterType.Detection)
			{
				result = CreateDetectionControl(project, paramter);
			}
			else if (paramter is HRMOption)
			{
				TabControlEx tabControlEx = new TabControlEx();
				tabControlEx.Style = Utility.GetResource("TabControlStylesmall") as Style;
				TabItem tabItem = new TabItem();
				tabItem.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem.Content = CreateBasicOptionControl(project, paramter);
				tabItem.Header = Utility.GetMessage("Prj_BasicParameter");
				tabControlEx.Items.Add(tabItem);
				tabItem = new TabItem();
				tabItem.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem.Content = CreateMeltingOptionControl(project, paramter);
				tabItem.Header = Utility.GetMessage("melting_analysis_parameter");
				tabControlEx.Items.Add(tabItem);
				tabItem = new TabItem();
				tabItem.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem.Content = CreateHRMOptionControl(project, paramter);
				tabItem.Header = Utility.GetMessage("hrm_analysis_parameter");
				tabItem.IsSelected = true;
				tabControlEx.Items.Add(tabItem);
				result = tabControlEx;
			}
			else if (paramter is MeltingOption)
			{
				TabControlEx tabControlEx2 = new TabControlEx();
				tabControlEx2.Style = Utility.GetResource("TabControlStylesmall") as Style;
				TabItem tabItem2 = new TabItem();
				tabItem2.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem2.Content = CreateBasicOptionControl(project, paramter);
				tabItem2.Header = Utility.GetMessage("Prj_BasicParameter");
				tabControlEx2.Items.Add(tabItem2);
				tabItem2 = new TabItem();
				tabItem2.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem2.Content = CreateMeltingOptionControl(project, paramter);
				tabItem2.Header = Utility.GetMessage("melting_analysis_parameter");
				tabItem2.IsSelected = true;
				tabControlEx2.Items.Add(tabItem2);
				result = tabControlEx2;
			}
			else if (paramter is ADOption)
			{
				TabControlEx tabControlEx3 = new TabControlEx();
				tabControlEx3.Style = Utility.GetResource("TabControlStylesmall") as Style;
				TabItem tabItem3 = new TabItem();
				tabItem3.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem3.Content = CreateBasicOptionControl(project, paramter);
				tabItem3.Header = Utility.GetMessage("Prj_BasicParameter");
				tabControlEx3.Items.Add(tabItem3);
				tabItem3 = new TabItem();
				tabItem3.Style = Utility.GetResource("TabItemStylesmall") as Style;
				tabItem3.Content = CreateADOptionControl(project, paramter);
				tabItem3.Header = Utility.GetMessage("ad_analysis_parameter");
				tabItem3.IsSelected = true;
				tabControlEx3.Items.Add(tabItem3);
				result = tabControlEx3;
			}
			else if (paramter is BasicOption)
			{
				result = CreateBasicOptionControl(project, paramter);
			}
			else if (paramter is CrossTalk)
			{
				result = CreateCrossTalkControl(project, paramter);
			}
			else if (paramter is Project)
			{
				result = CreateRuleControl(project, paramter);
			}
			return result;
		}

		private bool ValidateControl(object ctl)
		{
			if (ctl is UCProjectBasicOption)
			{
				int cycles = 0;
				if (Subset.GetAllProject().Count > 0)
				{
					cycles = Subset.GetAllProject()[0].PCRProgram.GetAcquisitionCycleCount();
				}
				if (!((UCProjectBasicOption)ctl).Validate(cycles))
				{
					return false;
				}
			}
			else if (ctl is TabControl)
			{
				foreach (TabItem item in (IEnumerable)((TabControl)ctl).Items)
				{
					if (item.Content is UCProjectBasicOption)
					{
						int cycles2 = 0;
						if (Subset.GetAllProject().Count > 0)
						{
							cycles2 = Subset.GetAllProject()[0].PCRProgram.GetAcquisitionCycleCount();
						}
						if (!((UCProjectBasicOption)item.Content).Validate(cycles2))
						{
							item.IsSelected = true;
							return false;
						}
					}
					else if (item.Content is UCMeltingOption)
					{
						if (!((UCMeltingOption)item.Content).Validate())
						{
							item.IsSelected = true;
							return false;
						}
					}
					else if (item.Content is UCHRMOption)
					{
						if (!((UCHRMOption)item.Content).Validate())
						{
							item.IsSelected = true;
							return false;
						}
					}
					else if (item.Content is UCADOption && !((UCADOption)item.Content).Validate())
					{
						item.IsSelected = true;
						return false;
					}
				}
			}
			else if (ctl is UCProjectDetection)
			{
				if (!((UCProjectDetection)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCProjectCrossTalk)
			{
				if (!((UCProjectCrossTalk)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCRule && !((UCRule)ctl).Validate())
			{
				return false;
			}
			return true;
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			if (htObject.Count == 1)
			{
				object ctl = grid.Children[grid.Children.Count - 1];
				if (!ValidateControl(ctl))
				{
					return;
				}
			}
			else
			{
				foreach (TabItem item in (IEnumerable)tabControl.Items)
				{
					UIElement ctl2 = item.Content as UIElement;
					if (!ValidateControl(ctl2))
					{
						item.IsSelected = true;
						return;
					}
				}
			}
			foreach (Project key in htObject.Keys)
			{
				Subset.SubsetParamter subsetParamter = Subset.GetSubsetParamter(key);
				object obj = htObject[key];
				if (obj is BasicOption)
				{
					subsetParamter.BasicOption = (BasicOption)obj;
				}
				else if (obj is CrossTalk)
				{
					subsetParamter.CrossTalk = (CrossTalk)obj;
				}
				else if (obj is Project)
				{
					subsetParamter.Rule = ((Project)obj).Rule;
				}
			}
			base.DialogResult = true;
			Close();
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
